package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/12/2 15:54
 * @Version 1.0
 */
@Api(tags = "spu商品")
@RestController
@RequestMapping("admin/product")
public class SpuInfoController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @ApiOperation("spu分页查询")
    @GetMapping("{page}/{limit}")
    public Result spuInfoPage(@RequestParam Long category3Id,
                              @PathVariable("page") Long page,
                              @PathVariable("limit") Long limit){

        Page<SpuInfo> spuInfoPage = spuInfoService.lambdaQuery().eq(SpuInfo::getCategory3Id, category3Id).page(new Page<SpuInfo>(page, limit));

        return Result.ok(spuInfoPage);
    }

    //admin/product/saveSpuInfo
    @ApiOperation("添加Spu")
    @PostMapping("/saveSpuInfo")
    public Result save(@RequestBody SpuInfoVo spuInfoVo){
        spuInfoService.saveSpuInfoData(spuInfoVo);
        return Result.ok();
    }

    //admin/product/spuImageList/{spuId}
    @ApiOperation("获取spu图片")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId")Long spuId){
        List<SpuImage> list = spuImageService.list(new QueryWrapper<SpuImage>().eq("spu_id",spuId));
        return Result.ok(list);
    }

    //admin/product/spuSaleAttrList/{spuId}
    @ApiOperation("获取spu商品属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId")Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

}
